package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController {
    @Reference
    private UserService userService;

    //分页查询
    @GetMapping("/page")
    public Result findPage(QueryPageBean queryPageBean) {
        try {
            log.info("------UserController------findPage------前端传过来的queryPageBean------" + queryPageBean);
            PageResult pageResult = userService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            log.info("------UserController------findPage------查询到的用户信息pageResult------" + pageResult);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_USER_FAIL);
        }
    }

    //根据id查询(在更新用户信息时,获取前端传来的id查询用户信息)
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            log.info("------UserController------findById------前端传过来的用户id------" + id);
            User user = userService.findById(id);
            log.info("------UserController------findById------通过id查询到的用户信息user------" + user);
            return new Result(true, MessageConstant.QUERY_USER_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }


    //查询用户对应角色的id数组(在更新用户信息时,查询所有findAllRole后,获取用户id查询对应角色roleId的数组返回给前端)
    @GetMapping( "/{id}/roleIds")
    public Result selectedRoleId(@PathVariable("id") Integer id) {
        try {
            log.info("------UserController------selectedRoleId------用户id------" + id);
            Integer [] roleIds = userService.selectedRoleId(id);
            for (Integer roleId : roleIds) {
                log.info("------UserController------selectedRoleId------用户对应的roleId------"+roleId);
            }
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }


    //新增用户
    @PostMapping("")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        try {
            log.info("------UserController------add------前端新增的用户信息user------" + user + "------设置的角色roleId------" + roleIds);
            userService.add(user, roleIds);
            return new Result(true, MessageConstant.ADD_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_USER_FAIL);
        }
    }

    //更新用户信息(前端点击编辑框的确定后执行)
    @PutMapping("")
    public Result update(@RequestBody User user, Integer[] roleIds) {
        try {
            log.info("------UserController------update-----更新后的数据user------" + user);
            for (Integer roleId : roleIds) {
              log.info("------UserController------update------设置的角色roleId------" + roleId);
            }
            userService.update(user, roleIds);
            return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.EDIT_USER_FAIL);
        }
    }

    //根据id删除用户
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        try {
            log.info("------UserController------deleteById-----被删除用户的id------" + id);
            userService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }




}
